/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.mapping;

import info.xmlprint.core.format.Format;
import info.xmlprint.core.format.FormatGroup;
import info.xmlprint.core.mapping.Mapping;
import info.xmlprint.core.mapping.MappingGroup;
import info.xmlprint.core.project.PrintProject;
import info.xmlprint.core.xml.PElementGroup;
import info.xmlprint.run.Constants;
import info.xmlprint.ui.message.GuiMessages;
import info.xmlprint.ui.widgets.IObserverControl;
import info.xmlprint.ui.widgets.ISetupComposite;
import info.xmlprint.util.ResizeColumns;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

/*
 * MappingGroup einstellen
 */

public class MappingGroupSetup extends Composite implements IObserverControl,
		ISetupComposite {

	private MappingGroupSetup _this = this;

	private ComboBoxCellEditor column2CellEditor;

	private ComboViewer formatComboViewer;

	private FormatGroup formatGroup;

	private MappingGroup mappingGroup;

	private MappingGroupSetup mappingGroupViewer = this;

	private TreeViewer mappingTreeViewer;

	private PElementGroup pElementGroup;
	
	private PrintProject printProject;

	private class comboCellModifier implements ICellModifier {
		private Tree tree;

		public comboCellModifier(Tree tree) {
			this.tree = tree;
		}

		@Override
		public boolean canModify(Object element, String colName) {
			if (element instanceof Mapping) {
				if (colName.equals("c2"))
					return true;
			}
			return false;
		}

		private int getNameIndex(String name) {
			if (formatGroup.getCount() > 0)
				for (int i = 0; i < formatGroup.getFormatNames().length; i++) {
					if (formatGroup.getFormatNames()[i].equals(name))
						return i;
				}
			return -1;
		}

		@Override
		public Object getValue(Object element, String property) {
			if (!(element instanceof Mapping))
				return -1;

			TreeItem tItem = tree.getSelection()[0];

			if (property.equals("c1")) {
				return tItem.getText();
			} else if (property.equals("c2")) {
				return getNameIndex(tItem.getText(1));
			}
			return null;
		}

		@Override
		public void modify(Object element, String property, Object value) {

			if (!(element instanceof TreeItem))
				return;

			TreeItem ti = (TreeItem) element;
			if (property.equals("c2")) {
				Integer i = (Integer) value;
				if (i < 0)
					return;

				Mapping mapping = (Mapping) ti.getData();

				Format format = formatGroup.getFormatByName(formatGroup
						.getFormatNames()[i]);
				mapping.setFormat(format);
				// notifyChanged();
			}
		}
	}

	private class treeViewerContentProvider implements ITreeContentProvider {

		@Override
		public void dispose() {
		}

		@Override
		public Object[] getChildren(Object parentElement) {
			return null;
		}

		@Override
		public Object[] getElements(Object list) {
			if (list instanceof MappingGroup) {
				return ((MappingGroup) list).getList().toArray();
			}
			return null;
		}

		@Override
		public Object getParent(Object element) {
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {
			return false;
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	private class treeViewerLabelProvider implements ITableLabelProvider {

		@Override
		public void addListener(ILabelProviderListener listener) {
		}

		@Override
		public void dispose() {
		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof Mapping) {
				Mapping mp = (Mapping) element;
				if (columnIndex == 0)
					return mp.getName();
				if (columnIndex == 1) {
					String s = null;

					Format f = mp.getFormat();
					if (f != null)
						s = f.getName();
					return s;
					// return mp.getFormat().getName();
				}
			}
			return null;
		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
		}
	}

	public MappingGroupSetup(Composite parent, int style) {
		super(parent, style);
		this.setLayout(new FillLayout());
		createControls();
	}

	public void createControls() {
		final ViewForm viewForm = new ViewForm(this, SWT.NONE);
		viewForm.setLayout(new GridLayout());

		// header
		Composite headComp = new Composite(viewForm, SWT.NONE);
		headComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		{
			headComp.setLayout(new GridLayout(11, false));

			Button newMapping = new Button(headComp, SWT.NONE); 
			Image newImg = new Image(null, this.getClass().getResourceAsStream(
				Constants.MAPPING_NEW_IMG));

			newMapping.setImage(newImg);

			newMapping.setToolTipText(GuiMessages.Stileditor_Gui_Mapping_new);
			newMapping.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event e) {
					MappingSetup ns=MappingSetup.getInstance(); 
					ns.registerObserver(_this);
					ns.setInput(printProject, new Mapping());
					ns.open(); 
				}
			});
			/*
			Button saveButton = new Button(headComp, SWT.NONE);
			Image saveImg = new Image(null, this.getClass()
					.getResourceAsStream(Constants.MAPPING_SAVE_IMG));
			saveButton.setImage(saveImg);
			saveButton.setToolTipText(GuiMessages.Stileditor_Gui_Mapping_save);
			saveButton.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event e) {
					FileDialog dialog = new FileDialog(viewForm.getShell(),
							SWT.SAVE);
					dialog.setFilterNames(new String[] { ".xml" });
					dialog.setFilterExtensions(new String[] { "*.xml", "*.*" });

					String filePath = dialog.open();

					if (filePath != null) {
						if (mappingGroup.saveToXml(filePath))
							MessageDialog
									.openInformation(
											null,
											"",
											GuiMessages.General_Gui_Mappings
													+ " "
													+ GuiMessages.General_Gui_Save_Success
													+ " in " + filePath);
						else
							MessageDialog.openInformation(null, "",
									GuiMessages.Stileditor_Error_ErrorSaving
											+ " "
											+ GuiMessages.General_Gui_Mappings
											+ " in " + filePath);

					}
				}
			});

			Button openButton = new Button(headComp, SWT.NONE);
			Image openImg = new Image(null, this.getClass()
					.getResourceAsStream(Constants.MAPPING_OPEN_IMG));
			openButton.setImage(openImg);
			openButton.setToolTipText(GuiMessages.Stileditor_Gui_Mapping_open);
			openButton.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event e) {
					FileDialog dialog = new FileDialog(viewForm.getShell(),
							SWT.OPEN);
					dialog.setFilterNames(new String[] { ".xml" });
					dialog.setFilterExtensions(new String[] { "*.xml", "*.*" });

					String fileName = dialog.open();

					if (fileName != null) {
						mappingGroup.readXmlFile(fileName, true,
								mappingGroupViewer); 
					}
				}
			});*/

			new Label(headComp, SWT.NONE).setText(" ");
			final Button allButton = new Button(headComp, SWT.CHECK);
			allButton.setText(GuiMessages.General_Gui_All);
			allButton.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event e) {
					if (allButton.getSelection())
						mappingTreeViewer.getTree().selectAll();

					else
						mappingTreeViewer.getTree().deselectAll();

				}
			});

			new Label(headComp, SWT.NONE)
					.setText(GuiMessages.Stileditor_Gui_Filter + ": ");
			final Text filterText = new Text(headComp, SWT.BORDER);
			filterText.setMessage(GuiMessages.Stileditor_Gui_Filter_Message);
			filterText.setLayoutData(new GridData());

			filterText.addListener(SWT.KeyUp, new Listener() {

				@Override
				public void handleEvent(Event event) {
					// notifyChanged();

					Tree tree = mappingTreeViewer.getTree();

					String s = filterText.getText().trim();
					if (s == null || s.equals("")) {
						setTreeItemDeactivate(tree);
						return;
					}
					mappingTreeViewer.expandAll();
					for (TreeItem ti : tree.getItems()) {
						if (ti.getText().startsWith(s))
							continue;

						if (ti.getItemCount() > 0) {
							boolean b = false;
							for (TreeItem t2 : ti.getItems()) {
								if (t2.getText().startsWith(s)) {
									b = true;
									break;
								}
							}
							if (b)
								continue;
						}

						ti.dispose();
					}
					setTreeItemDeactivate(tree);

				}
			});

			// TODO: The filter does not work.
			/*
			 * new Label(headComp, SWT.NONE)
			 * .setText(GuiMessages.Stileditor_Gui_Format);
			 * 
			 * formatComboViewer = new ComboViewer(headComp, SWT.NONE);
			 * setFormatComboViewer(formatComboViewer);
			 * 
			 * formatComboViewer.setInput(formatGroup);
			 * 
			 * final Combo formatCombo = formatComboViewer.getCombo();
			 * 
			 * formatCombo.addListener(SWT.Selection, new Listener() {
			 * 
			 * @Override public void handleEvent(Event e) { IStructuredSelection
			 * selection = (IStructuredSelection) formatComboViewer
			 * .getSelection();
			 * 
			 * Format format = (Format) selection.getFirstElement();
			 * 
			 * 
			 * // String formatName = formatCombo.getItem(formatCombo //
			 * .getSelectionIndex()); // if (formatName != null) for (TreeItem
			 * ti : mappingTreeViewer.getTree() .getSelection()) { ((Mapping)
			 * ti.getData()).setFormat(format); // if (ti.getChecked()) //
			 * ((Mapping) ti.getData()).setFormatName(formatName);
			 * notifyChanged(); }
			 * 
			 * } });
			 */

			/*
			 * Inhalt von alle style (Zuordnungen)anzuzeigen, //Button
			 * infoButton = new Button(headComp, SWT.NONE);
			 * infoButton.setText("?"); infoButton
			 * .setToolTipText(GuiMessages.Stileditor_Gui_Tooltip_Mappings);
			 * infoButton.addSelectionListener(new SelectionAdapter() {
			 * 
			 * @Override public void widgetSelected(SelectionEvent e) {
			 * showInfo(XmlUtil.doc2IndentString(mappingGroup.getXmlDoc())); }
			 * 
			 * });
			 * 
			 * // Inhalt von xsl(fuer FO erzeugen) anzuzeigen. Button
			 * infoButton2 = new Button(headComp, SWT.NONE);
			 * infoButton2.setText("?");
			 * infoButton2.setToolTipText(GuiMessages.Stileditor_Gui_Tooltip_XSLT
			 * ); infoButton2.addSelectionListener(new SelectionAdapter() {
			 * 
			 * @Override public void widgetSelected(SelectionEvent e) {
			 * showInfo(XmlUtil.doc2IndentString(mappingGroup.getXslDoc())); }
			 * 
			 * });
			 */
			headComp.pack();
		}// header end

		Composite tvComp = new Composite(viewForm, SWT.NONE);
		tvComp.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Tree der XML-Elementen
		mappingTreeViewer = new TreeViewer(tvComp, SWT.FULL_SELECTION
				| SWT.MULTI);
		final Tree tree = mappingTreeViewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		TableLayout tableLayout = new TableLayout();
		tree.setLayout(tableLayout);

		/*
		 * ColumnWeightData col1 = new ColumnWeightData(headComp.getSize().x /
		 * 2); tableLayout.addColumnData(col1); ColumnWeightData col2 = new
		 * ColumnWeightData(headComp.getSize().x / 2);
		 * tableLayout.addColumnData(col2);
		 */

		new TreeColumn(tree, SWT.CENTER)
				.setText(GuiMessages.Stileditor_Gui_Description);
		new TreeColumn(tree, SWT.CENTER)
				.setText(GuiMessages.Stileditor_Gui_Format);

		// CellEditor
		{
			mappingTreeViewer.setColumnProperties(new String[] { "c1", "c2" });
			column2CellEditor = new ComboBoxCellEditor(tree, new String[] {},
					SWT.READ_ONLY);

			CellEditor[] cellEditor = new CellEditor[] { null,
					column2CellEditor };
			mappingTreeViewer.setCellEditors(cellEditor);
			mappingTreeViewer.setCellModifier(new comboCellModifier(tree));
		}

		// ContentProvider
		mappingTreeViewer.setContentProvider(new treeViewerContentProvider());

		// LabelProvider
		mappingTreeViewer.setLabelProvider(new treeViewerLabelProvider());

		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				TreeItem treeItem = tree.getItem(new Point(e.x, e.y));
				if (treeItem == null) {
					tree.deselectAll();
					return;
				}
			}
		});

		// Bei Doppelklick neue Zuweisung erstellen (MS)
		tree.addListener(SWT.MouseDoubleClick, new Listener() {
			@Override
			public void handleEvent(Event e) {
				TreeItem treeItem = tree.getItem(new Point(e.x, e.y));
				MappingSetup ns =   MappingSetup.getInstance();
				if (treeItem == null) { // new mapping //TODO: Does not work anymore. Why?
					tree.deselectAll();
					ns.setInput(printProject, new Mapping());
					return;
				} else { // edit existing mapping
					Mapping es = (Mapping) treeItem.getData();
					ns.setInput(printProject, es);
				}
				ns.registerObserver(_this);
				ns.open();
			}
		});

		// Bei "Entf" Zuweisung löschen, "a" aktiviert, "d" deaktiviert Auswahl
		// Problem: Die Auswahl bleibt nicht auf bestehen

		tree.addListener(SWT.KeyDown, new Listener() {
			@Override
			public void handleEvent(Event e) {
				switch (e.keyCode) {
				case SWT.DEL:
					for (TreeItem ti2 : tree.getSelection()) {
						Mapping ep = (Mapping) ti2.getData();
						mappingGroup.getList().remove(ep);
					}
					// notifyChanged();
					break;
				case 97: // a
					for (TreeItem ti2 : tree.getSelection()) {
						Mapping ep = (Mapping) ti2.getData();
						ep.setActive(true);

						setTreeItemStyle(ti2, SWT.NONE, SWT.COLOR_BLACK);
					}
					break;
				case 100: // d
					for (TreeItem ti2 : tree.getSelection()) {

						Mapping ep = (Mapping) ti2.getData();
						ep.setActive(false);

						setTreeItemStyle(ti2, SWT.ITALIC, SWT.COLOR_DARK_GRAY);
					}
					break;
				}
			}
		});

		tree.addListener(SWT.MenuDetect, new Listener() {
			@Override
			public void handleEvent(Event event) {
				Menu menu = new Menu(tree.getShell(), SWT.POP_UP);
				MenuItem newItem = new MenuItem(menu, SWT.PUSH);
				newItem.setText(GuiMessages.Stileditor_Gui_Mapping_new);
				newItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						MappingSetup ns = MappingSetup.getInstance();
						ns.registerObserver(_this);
						ns.setInput(printProject, new Mapping());
						ns.open();
					}
				});

				menu.setVisible(true);

				if (tree.getSelection() == null || tree.getSelectionCount() < 1)
					return;

				final TreeItem ti = tree.getSelection()[0];

				if (ti == null) {
					return;
				}

				Mapping currEp = (Mapping) ti.getData();

				new MenuItem(menu, SWT.SEPARATOR);
				MenuItem editItem = new MenuItem(menu, SWT.PUSH);
				editItem.setText(GuiMessages.General_Gui_Edit);
				editItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						Mapping es = (Mapping) ti.getData();
						MappingSetup ns =MappingSetup.getInstance();
						ns.registerObserver(_this);
						ns.setInput(printProject, es);
						ns.open();
					}
				});
				if (tree.getSelection().length > 1)
					editItem.setEnabled(false);

				MenuItem activeItem = new MenuItem(menu, SWT.PUSH);
				activeItem.setText(GuiMessages.Stileditor_Gui_Activate);
				activeItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						for (TreeItem ti2 : tree.getSelection()) {
							Mapping ep = (Mapping) ti2.getData();
							ep.setActive(true);

							setTreeItemStyle(ti2, SWT.NONE, SWT.COLOR_BLACK);
						}
					}
				});

				if (currEp.getActive())
					activeItem.setEnabled(false);

				MenuItem disActiveItem = new MenuItem(menu, SWT.PUSH);
				disActiveItem.setText(GuiMessages.Stileditor_Gui_Deactivate);
				disActiveItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						for (TreeItem ti2 : tree.getSelection()) {

							Mapping ep = (Mapping) ti2.getData();
							ep.setActive(false);

							setTreeItemStyle(ti2, SWT.ITALIC,
									SWT.COLOR_DARK_GRAY);
						}
					}
				});

				if (!currEp.getActive())
					disActiveItem.setEnabled(false);

				new MenuItem(menu, SWT.SEPARATOR);
				MenuItem toTopItem = new MenuItem(menu, SWT.PUSH);
				toTopItem.setText(GuiMessages.General_Gui_Top);
				// toTopItem.setEnabled(false);
				toTopItem.setText(GuiMessages.General_Gui_Top);
				moveMapping(toTopItem, GuiMessages.General_Gui_Top, tree);

				MenuItem upItem = new MenuItem(menu, SWT.PUSH);
				upItem.setText(GuiMessages.General_Gui_Up);
				if (tree.getSelectionCount() > 1)
					upItem.setEnabled(false);
				moveMapping(upItem, GuiMessages.General_Gui_Up, tree);

				MenuItem downItem = new MenuItem(menu, SWT.PUSH);
				downItem.setText(GuiMessages.General_Gui_Down);
				if (tree.getSelectionCount() > 1)
					downItem.setEnabled(false);
				moveMapping(downItem, GuiMessages.General_Gui_Down, tree);

				MenuItem bottomItem = new MenuItem(menu, SWT.PUSH);
				bottomItem.setText(GuiMessages.General_Gui_Bottom);
				moveMapping(bottomItem, GuiMessages.General_Gui_Bottom, tree);

				new MenuItem(menu, SWT.SEPARATOR);

				MenuItem delItem = new MenuItem(menu, SWT.PUSH);
				delItem.setText(GuiMessages.General_Gui_Delete);

				delItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						for (TreeItem ti2 : tree.getSelection()) {
							Mapping ep = (Mapping) ti2.getData();
							mappingGroup.getList().remove(ep);
						}
						updateControl();
					}
				});

			}
		});

		viewForm.setContent(tvComp);
		viewForm.setTopLeft(headComp);
		viewForm.pack();

		tvComp.addControlListener(new ResizeColumns(tvComp, tree));

	}

	public FormatGroup getFormatGroup() {
		return formatGroup;
	}

	public MappingGroup getMappingGroup() {
		return mappingGroup;
	}

	public PElementGroup getPElementGroup() {
		return pElementGroup;
	}

	// public List<String> getUsedFormatNames() {
	// List<String> formatNamesList = new ArrayList<String>();
	//
	// for (TreeItem ti : mappingTreeViewer.getTree().getItems()) {
	// formatNamesList.add(ti.getText(1));
	// }
	// return formatNamesList;
	// }
	//
	// public boolean isFormatInUsed(String formatName) {
	// for (TreeItem ti : mappingTreeViewer.getTree().getItems()) {
	// if (ti.getText(1).equals(formatName)) {
	// MessageDialog.openError(null, null,
	// GuiMessages.Stileditor_Error_FormatUsed1 + " "
	// + formatName + " "
	// + GuiMessages.Stileditor_Error_FormatUsed2);
	// return true;
	// }
	// }
	// return false;
	// }

	private void moveMapping(MenuItem mi, final String typ, final Tree tree) {
		mi.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List<Mapping> mpList = mappingGroup.getList();

				if (typ.equals(GuiMessages.General_Gui_Top)) {
					TreeItem[] tis = tree.getSelection();
					for (int i = tis.length - 1; i >= 0; i--) {
						Mapping seleMapp = (Mapping) tis[i].getData();

						mpList.remove(seleMapp);
						mpList.add(0, seleMapp);
					}
				} else if (typ.equals(GuiMessages.General_Gui_Bottom)) {
					for (TreeItem ti2 : tree.getSelection()) {
						Mapping currSt = (Mapping) ti2.getData();
						int length = mpList.size();
						mpList.remove(currSt);
						mpList.add(length - 1, currSt);
					}
				} else if (typ.equals(GuiMessages.General_Gui_Up)) {
					Mapping currSt = (Mapping) tree.getSelection()[0].getData();
					int index = mpList.indexOf(currSt);
					if (index > 0) {
						Mapping temp = mpList.get(index - 1);
						mpList.set(index - 1, currSt);
						mpList.set(index, temp);
					}

				} else if (typ.equals(GuiMessages.General_Gui_Down)) {
					Mapping currSt = (Mapping) tree.getSelection()[0].getData();
					int index = mpList.indexOf(currSt);
					if (index + 1 < mpList.size()) {
						Mapping temp = mpList.get(index + 1);
						mpList.set(index + 1, currSt);
						mpList.set(index, temp);
					}
				}

				// mappingTreeViewer.setInput(mpList);
				  updateControl();
			}
		});
	}

	private void setColumnCellEditor() {
		if (formatGroup.getCount() > 0) {
			column2CellEditor.setItems(formatGroup.getFormatNames());
		} else {
			column2CellEditor.setItems(new String[] {});
		}
	}

	private void setFormatComboViewer(ComboViewer cv) {
		// Set FormatCombo Content
		cv.setContentProvider(new IStructuredContentProvider() {
			@Override
			public void dispose() {
			}

			@Override
			public Object[] getElements(Object inputElement) {
				if (inputElement instanceof FormatGroup) {
					return ((FormatGroup) inputElement).getFormats();
				}
				return null;
			}

			@Override
			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
			}
		});

		// Set FormatCombo Label
		cv.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object input) {
				if (input instanceof Format) {
					return ((Format) input).getName();
				}
				return null;
			}
		});

	}

	@Override
	public void setPrintProject(PrintProject pProject) {
		printProject = pProject;
		pElementGroup = pProject.getPElementGroup();
		mappingGroup = pProject.getMappingGroup();
		formatGroup = pProject.getFormatGroup();
		mappingTreeViewer.setInput(mappingGroup);
		setTreeItemDeactivate(mappingTreeViewer.getTree());
		setColumnCellEditor();
	}

	private void setTreeItemDeactivate(Tree tree) {
		for (TreeItem ti : tree.getItems()) {
			Mapping ep = (Mapping) ti.getData();
			if (!ep.getActive()) {
				setTreeItemStyle(ti, SWT.ITALIC, SWT.COLOR_DARK_GRAY);
			} else {
				setTreeItemStyle(ti, SWT.NONE, SWT.COLOR_BLACK);
			}
		}

	}

	private void setTreeItemStyle(TreeItem ti, int swtFont, int swtColor) {
		FontData fontData = ti.getFont().getFontData()[0];
		Font font = new Font(ti.getDisplay(), new FontData(fontData.getName(),
				fontData.getHeight(), swtFont));
		ti.setFont(font);
		ti.setForeground(ti.getDisplay().getSystemColor(swtColor));
	}

	@Override
	public void updateControl() {
		Tree tree = mappingTreeViewer.getTree();
		for (TreeItem ti : tree.getItems()) {
			Mapping st = (Mapping) ti.getData();
			if (!st.getActive()) {
				setTreeItemStyle(ti, SWT.ITALIC, SWT.COLOR_DARK_GRAY);
			} else
				setTreeItemStyle(ti, SWT.NONE, SWT.COLOR_BLACK);
		}
		setColumnCellEditor();
		mappingTreeViewer.refresh();
		// formatComboViewer.refresh(); //TODO: deactivated for now
	}
}
